package com.company.class5;

import com.company.class4.Washmachine;

import java.util.Arrays;

/**
 * @Classname ArrayTest
 * @Description 数组
 * @Date 2022/4/24 17:46
 * @Created by zbw
 */

public class ArrayTest {
    public static void main(String[] args) {
        //定义一个数组，长度为8
        int[] iarr = new int[8];
        System.out.println(Arrays.toString(iarr));
        //通过大括号赋初值，这是一个长度为0的数组
        int[] iarrinit = {};
        //{}里面表示的是元素的内容，用,分割
        int[] iarrinitL = {1,2,3,4,565,66};
        //完整的格式
        int[] iarrinitLe = new int[]{1,2,3,4,6,7};
        System.out.println(iarrinitLe);
        // 如果想存不同类型到数组中，就用Object数组
        Object[] o = new Object[]{"zbw",1,new Washmachine()};


        //数组一旦定义之后，就只能存对应类型的数据
//        String[] sarr = {"roy","will","土匪","zbw",1};

        String[] sarr = new String[9];
        System.out.println(Arrays.toString(sarr));
        //类类型的数组
        Washmachine zbwwm = new Washmachine();
        System.out.println("输出一台洗衣机"+zbwwm);
        Washmachine[] wmArr = {zbwwm,new Washmachine(),new Washmachine()};
        System.out.println("输出一台洗衣机"+wmArr.toString());

        //查一个元素
        //通过数组下标进行取值，编程语言中数数从0开始数，下标从0开始，到length-1结束
        System.out.println(iarrinitLe[1]);

        //修改一个元素的值，直接赋值就好
        iarrinitLe[3] = 333;
        System.out.println(iarrinitLe[3]);
        //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
        //	at com.company.class5.ArrayTest.main(ArrayTest.java:45)
        //下标越界异常报错
//        System.out.println(iarrinitLe[6]);

        //通过Arrays类中的toString方法，将数组转换成可输出的内容
        System.out.println(Arrays.toString(iarrinitLe));

        System.out.println("-----------------通过for进行遍历-----------------------------");
        for(int index = 0;index < iarrinitLe.length;index++){
            System.out.print(iarrinitLe[index]+",");
        }

        System.out.println();
        System.out.println("---------------------通过foreach进行遍历------------");
        //foreach增强型循环，遍历所有元素
        for(int i:iarrinitLe){
            System.out.print(i+",");
        }

        System.out.println();
        System.out.println("-------------------做一次排序-----------------");
        Arrays.sort(iarrinitLe);
        System.out.println(Arrays.toString(iarrinitLe));

    }
}
